<template>
  <div>
    <div>
      <el-form
        :inline="true"
        style="margin-top: 10px;margin-left: 10px"
        @keyup.enter.native="getAllList()"
      >
        <el-form-item>
          <el-input
            v-model="key"
            placeholder="通道名称"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-select v-model="queryType" placeholder="操作类型" clearable :style="{width: '100%'}">
            <el-option v-for="(item, index) in types" :key="index" :label="item.code"
                       :value="item.id" :disabled="item.disabled"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="getAllList()" >查询</el-button>
          <el-button type="primary"
                     @click="addOrUpdateHandle()"
          >新增</el-button
          >
          <el-button
            type="danger"
            :disabled="dataListSelections.length <= 0"
            @click="deleteHandle()"
          >批量删除</el-button
          >
        </el-form-item>
      </el-form>

    </div>
    <el-table
      :data="tableData"
      border
      @selection-change="selectionChangeHandle"
      style="width: 100%;align-content: center">

      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50"
      >
      </el-table-column>
      <el-table-column
        :show-overflow-tooltip="true"
        prop="name"
        label="名称"
        width="150">
      </el-table-column>
      <el-table-column

        prop="typeString"
        label="操作类型"
        :show-overflow-tooltip="true"
        width="120">
      </el-table-column>
      <el-table-column
        prop="host"
        :show-overflow-tooltip="true"
        label="主机ip"
        width="130">
      </el-table-column>
      <el-table-column
        prop="port"
        :show-overflow-tooltip="true"
        label="主机端口"
        width="80">
      </el-table-column>
      <el-table-column
        prop="dbtype"
        label="数据库类型"
        width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.dbtype==1">mysql</span>
          <span v-if="scope.row.dbtype==2">oracle</span>
          <span v-if="scope.row.dbtype==3">sqlServer</span>
        </template>

      </el-table-column>
      <el-table-column
        :show-overflow-tooltip="true"
        prop="dbname"
        label="数据库名称"
        width="150">
      </el-table-column>
      <el-table-column
        :show-overflow-tooltip="true"
        prop="path"
        label="文件路径"
        width="100">
      </el-table-column>
      <el-table-column
        :show-overflow-tooltip="true"
        prop="filename"
        label="文件名"
        width="100">
      </el-table-column>

      <el-table-column
        :show-overflow-tooltip="true"
        prop="description"
        label="描述"
        width="150">
      </el-table-column>
<!--      <el-table-column label="状态" align="center" prop="active">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.active"
            :active-value="1"
            :inactive-value="0"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>-->


      <el-table-column
        fixed="right"
        label="操作"
        width="230">
        <template slot-scope="scope">

          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="minute(scope.row)"
            v-hasPermi="['monitor:operlog:query']"
          >详细</el-button>
          <el-button
            v-if="scope.row.active===0"
            type="text"
            size="small"
            @click="handleStatusChange(scope.row,1)"
          >激活</el-button
          >
          <el-button
            v-if="scope.row.active===1"
            type="text"
            size="small"
            @click="handleStatusChange(scope.row,0)"
          >禁用</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="testChannel(scope.row)"
          >测试</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="addOrUpdateHandle(scope.row.id)"
          >修改</el-button
          >

          <el-button
            type="text"
            size="small"
            @click="deleteHandle(scope.row.id,scope.row.name)"
          >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageNum"
      :page-sizes="[8, 15, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      style="text-align: right;margin-right: 10px;margin-top: 10px"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getAllList"></add-or-update>

    <!-- 通道详细 -->
    <el-dialog title="通道详细" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="minuteFrom" label-width="150px" size="mini">
        <el-row>
          <el-col :span="12">
            <!--<el-form-item label="任务id：">{{ form.id }}</el-form-item>-->
            <el-form-item
              label="名称："
            >{{ minuteFrom.name }}</el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="操作类型:">{{ minuteFrom.typeString }}</el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="主机ip地址:">{{minuteFrom.host}}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="主机服务端口:">{{ minuteFrom.port }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="数据库类型:" v-if="minuteFrom.type==115">
              <span v-if="minuteFrom.dbtype==1">mysql</span>
              <span v-if="minuteFrom.dbtype==2">oracle</span>
              <span v-if="minuteFrom.dbtype==3">sqlServer</span>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="minuteFrom.type==115">
            <el-form-item label="数据库名称:">{{ minuteFrom.dbname }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="数据源用户名:">{{ minuteFrom.username }}</el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="数据源密码:">{{ minuteFrom.password }}</el-form-item>
          </el-col>
          <el-col :span="12" v-if="minuteFrom.type==117">
            <el-form-item label="文件路径:">{{ minuteFrom.path }}</el-form-item>
          </el-col>
          <el-col :span="12" v-if="minuteFrom.type==117">
            <el-form-item label="文件名:">{{ minuteFrom.filename }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="描述:">{{ minuteFrom.description }}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="open = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import AddOrUpdate from "./addOrUpdate";
export default {
  components: {
    AddOrUpdate,
  },

  methods: {
    testChannel(row){
      //测试连接
      this.$http({
        url: this.$http.adornUrl("/tbssbuschannel/connect"),
        method: "post",
        data: row
      }).then(({ data }) => {
        if (data && data.code === 0) {
          if(data.data) {
            this.$message({
              message: "连接成功",
              type: "success",
              duration: 1500
            });
          }else {
            this.$message({
              message: "连接失败",
              type: "error",
              duration: 1500
            });
          }
          this.getAllList()
        } else {
          this.$message.error(data.msg);
        }

      });

    },
    minute(row){
      this.open=true
      this.minuteFrom=row
    },
    handleStatusChange(row,value){
      //修改状态
      this.$http({
        url: this.$http.adornUrl("/tbssbuschannel/updateActive"),
        method: "get",
        params: this.$http.adornParams({
          id: row.id,
          active: value
        }),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          if(row.active==0) {
            this.$message({
              message: "激活成功",
              type: "success",
              duration: 1500
            });
          }else {
            this.$message({
              message: "禁用成功",
              type: "success",
              duration: 1500
            });
          }
          this.getAllList()
        } else {
          this.$message.error(data.msg);
        }

      });
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    getTypes(){
      this.$http({
        url: this.$http.adornUrl('/tbssbuschannel/dbtype'),
        method: 'get',
        params: this.$http.adornParams()
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.types = data.date;
        }
      })
    },
    getAllList(){
      this.dataListLoading=true
            this.$http({
              url: this.$http.adornUrl("/tbssbuschannel/list"),
              method: "get",
              params: this.$http.adornParams({
                page: this.pageNum,
                limit: this.pageSize,
                key: this.key,
                queryType:this.queryType
              }),
            }).then(({ data }) => {
              if (data && data.code === 0) {
                this.tableData = data.page.records;
                this.totalPage = data.page.total;
              } else {
                this.dataList = [];
                this.totalPage = 0;
              }
            });
      this.dataListLoading = false;
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageNum = val;
      this.getAllList();
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageNum = 1;
      this.getAllList();
    },
    // 删除
    deleteHandle(id,name) {
      var ids = id
        ? [id]
        : this.dataListSelections.map((item) => {
          return item.id;
        });
      var names = name
        ? [name]
        : this.dataListSelections.map((item) => {
          return item.name;
        });
      this.$confirm(
        /*`确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,*/
        `确定对[名称=${names.join(",")}]进行[${name ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
                this.$http({
                  url: this.$http.adornUrl("/tbssbuschannel/delete"),
                  method: "post",
                  data: this.$http.adornData(ids, false),
                }).then(({ data }) => {
                  if (data && data.code === 0) {
                    this.$message({
                      message: "操作成功",
                      type: "success",
                      duration: 1500,
                      onClose: () => {
                        this.getAllList();
                      },
                    });
                  } else {
                    this.$message.error(data.msg);
                  }
                });
      });
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    //获取子组件传递过来的值
    handleVisible(data) {
      // console.log(data);
      this.addOrUpdateVisible = data;
      this.getAllList();
    }
  },
  data() {
    return {
      open:false,
      minuteFrom:{},
      queryType:"",
      types:[],
      addOrUpdateVisible:false,
      dataListLoading: false,
      dataListSelections: [],
      key:"",
      pageNum:1,
      pageSize:8,
      totalPage:1,
      tableData: []
    }
  },
  created() {
    this.dataListLoading=true
    this.getAllList();
    this.getTypes();
  }
}
</script>

<style scoped>

</style>
